<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/25
 * Time: 21:05
 */
namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

class Brand extends Model
{
    use SoftDelete;
    protected $deleteTime='delete_time';
    protected $pk='brand_id';
    public function searchRecommendedAttr($query,$value)
    {
        if (isset($value)&&$value != '')    $query->where('recommended','=',$value);
    }
    public function searchIfShowAttr($q,$v)
    {
        if (isset($v) && $v!= '')        $q->where('if_show','=',$v);
    }
    public function searchKeywordsAttr($q,$v)
    {
        if (isset($v) && $v!= '')        $q->where('brand_name','like',"%{$v}%");
    }
    public static function getBrandList($where,$keywords)
    {
        $list=Brand::field(true)
            ->order('sort_order')
            ->withSearch(array_keys($where),$where)
            ->paginate([
            'list_rows'=>10,
            'query'=>$where
            ])
            ->each(function ($item,$key) use($keywords){
                $new_key="<span style='color: red;'>{$keywords}</span>";
                $item['brand_name']=str_replace($keywords,$new_key,$item['brand_name']);
            });
        return  $list;
    }
    public static function  getBrandRecover($where,$keywords)
    {
        $list=Brand::onlyTrashed()
            ->field(true)
            ->order('sort_order')
            ->withSearch(array_keys($where),$where)
            ->paginate([
                'list_rows'=>10,
                'query'=>$where
            ])
            ->each(function ($item,$key) use($keywords){
                $new_key="<span style='color: red;'>{$keywords}</span>";
                $item['brand_name']=str_replace($keywords,$new_key,$item['brand_name']);
            });
        return  $list;
    }
}